<!DOCTYPE html>
<html lang="zh">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>学习助手</title>
    <link rel="stylesheet" type="text/css" href="/static/bootstrap/css/bootstrap.min.css">

    <link rel="stylesheet" type="text/css" href="/static/css/style.css">
    <link rel="icon" href="/static/img/logo.png" type="image/png">
</head>
<style>
    .title_label {
        left: 8px;
        top: 20px;
        width: 120px;
        height: 29px;
        color: rgba(255, 255, 255, 1);
        font-size: 20px;
        text-align: left;
        font-family: PingFangSC-bold;
    }

    .header_main {
        width: 100%;
        margin: auto;
        background-image: url(/static/img/head_bg.png);
        background-position: top -140px right 0px;
        height: auto;
        padding: 10px;
        background-size: cover;
        background-size: 100%;
        background-repeat: no-repeat;
        color: dimgray;

    }

    .workStyle>li {
        float: left;
        margin-left: 10px;
        color: #fff;
        font-weight: 700;
    }

    #infoDiv {
        margin-top: 80px;
        font-size: medium;
    }
</style>

<body id="mainAPP" class="header_main">
    <header id="home">

        <ul class="workStyle">
            <li>
                <!-- 今日，参数是当前时间往后退1天，所以参数天1 -->
                <button class="btn btn-success" onclick="window.location.href='/home'">个人信息 </button>

            </li>
            <li>
                <!-- 今日，参数是当前时间往后退1天，所以参数天1 -->
                <button class="btn btn-warning" onclick="window.location.href='/records'">加分记录 </button>

            </li>
            <li>
                <!-- 7天，参数是当前时间往后退7天，所以参数填7 -->
                <button class="btn btn-secondary" onclick="window.location.href='/fill'">填写加分 </button>
            </li>
        </ul>
    </header>
    <!-- 用户列表 div -->
    <div id="infoDiv">
        <span style="width: 100%; text-align: center; display: block;">选择本次加分科目</span>
        <hr>
        <div class="row ">
            <ul id="courseListUI">
            </ul>
        </div>
        <hr>
        <div style="margin-top: 20px;">
            <textarea class="  col-12 " id="otherInput" rows="2" placeholder="填写得分原因"></textarea>

        </div>
        <div>
            <input type="number" max="10" min="0" class="col-4 " step="1" placeholder="填写得分数" id="recordInput">
            <button class="btn btn-success  offset-1 col-5 " onclick="fillData()">提交 </button>

        </div>
    </div>

    <div style="position:absolute; bottom: 0px;left: 0; width: 100%;   background-color: #17ad74; " id="bottomBox">
        <div style="color: white; text-align: center;" id="userinfo">信息工程学院</div>
    </div>
    <script src="/static/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/static/js/axios.min.js"></script>
    <script type="text/javascript">
        function fillInfo() {
            let infoStr = sessionStorage.getItem("user");
            let info = JSON.parse(infoStr);
            document.getElementById("userinfo").innerText = info.Classname + "." + info.Name;
            let courseListStr = localStorage.getItem("courseList")
            let courseList = JSON.parse(courseListStr);
            let courseListHtml = "";
            const entries = Object.entries(courseList);
            for (const [id, course] of entries) {
                if (course.Grade != info.Grade) {
                    continue;
                }
                courseListHtml += `  <li> <input type="radio" name="course"   value="` + id + `">` + course.CourseCn + "<br/>" + course.CourseEn + `</input> </li>`;
            };
            document.getElementById("courseListUI").innerHTML = courseListHtml;
        }
        fillInfo();
      

        //userID 用户的编号
        function fillData() {
            // 1. 从session Storage 中获取token
            var jwt = sessionStorage.getItem("token");
            let recordInfo = new Object();
            recordInfo.record = document.getElementById("recordInput").value;
            recordInfo.comments = document.getElementById("otherInput").value;
            const selectedRadio = document.querySelector('input[name="course"]:checked');
            if (selectedRadio) { 
                recordInfo.course = parseInt(selectedRadio.value, 10);
            } else {
                alert("请选择本次加分的课程科目");
                return
            }
           
            recordInfo.record = parseInt(recordInfo.record, 10);
            if (recordInfo.record > 20) {
                alert("加分太多，有点异常");
                return
            }
            // 2. 使用axios 获取用户详情
            axios.post('/api/records/signIn', recordInfo,
                // 在header中设置jwt值
                {
                    headers: { 'token': jwt }
                }).then(function (response) {
                    // 判断返回的数据是否正常
                    if (response.status == 200) {
                        // 后端返回的用户信息 
                        if (response.data.code ==0){
                            alert("填写失败:"+response.data.data);
                        }else{
                            alert("填写成功");
                        } 
                    } else {
                        alert(response.data);
                    }
                }).catch(function (error) {
                    alert(error)
                });
        }

    </script>

</body>

</html>